//
//  BaseTabBarController.swift
//  suda
//
//  Created by youai on 2019/7/19.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit

class BaseTabBarController : UITabBarController {
    
    var titles:[String]
    var selectedImageNames:[UIImage]
    var normalImageNames:[UIImage]
    var vcList:[UIViewController]
    init(titles:[String], selectedImageNames:[UIImage], normalImageNames:[UIImage], vcList:[UIViewController]){
        assert(titles.count > 0)
        assert(selectedImageNames.count == titles.count  && normalImageNames.count == titles.count && vcList.count == titles.count)
        self.titles = titles
        self.selectedImageNames = selectedImageNames
        self.normalImageNames = normalImageNames
        self.vcList = vcList
        super.init(nibName: nil, bundle: nil)
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    override func viewDidLoad() {
        if #available(iOS 11.0, *) {
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        let apperance = UITabBar.appearance()
        apperance.isTranslucent = false
        
        let selectedTextAttr = [NSAttributedString.Key.foregroundColor:UIColor.xFC4C02,
                                NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10, weight: .medium)]
        let normalTextAttr = [NSAttributedString.Key.foregroundColor:UIColor.x666666,
                              NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10, weight: .medium)]
        
        if #available(iOS 13, *) {
            let appearance = UITabBarAppearance()

            appearance.backgroundColor = .white
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .white

            appearance.stackedLayoutAppearance.normal.iconColor = .x666666
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.x666666]

            appearance.stackedLayoutAppearance.selected.iconColor = .xFC4C02
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.xFC4C02]

            self.tabBar.standardAppearance = appearance
        }
        
        var vcs = [UIViewController]()
        for (idx,vc) in vcList.enumerated() {
            //            let nav = UINavigationController(rootViewController: vc)
            //            nav.setNavigationBarHidden(true, animated: false)
            vc.title=titles[idx]
            vc.tabBarItem.image = normalImageNames[idx].withRenderingMode(.alwaysOriginal)
            vc.tabBarItem.selectedImage = selectedImageNames[idx].withRenderingMode(.alwaysOriginal)
            
//            vc.tabBarItem.image = ImageHelper.resize(normalImageNames[idx], to: CGSize(width: 24, height: 24)).withRenderingMode(.alwaysOriginal)
//            vc.tabBarItem.selectedImage = ImageHelper.resize(selectedImageNames[idx], to: CGSize(width: 24, height: 24)).withRenderingMode(.alwaysOriginal)
            vc.tabBarItem.title = titles[idx]
            vc.tabBarItem.setTitleTextAttributes(normalTextAttr, for: .normal)
            vc.tabBarItem.setTitleTextAttributes(selectedTextAttr, for: .selected)
            vcs.append(vc)
        }

        self.setViewControllers(vcs, animated: false)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
